﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class InventoryManage : MonoBehaviour
{
    static InventoryManage instance;

    public BagSaveGoods myBag;
    public GameObject slotGrid;
    public Slot slotPrefab;
    public Text itemInformation;
    void Awake()
        {
        if(instance != null)
            Destroy(this);
        instance=this;
    }
    private void OnEnable()
        {
        RefreshItem();
        instance.itemInformation.text="";
    }
    public static void UpadteItemInfo(string itemDescription)
        {
        instance.itemInformation.text=itemDescription;
    }
    public static void  CreateNewItem(Item item)
        {
         Slot newItem=Instantiate(instance.slotPrefab,instance.slotGrid.transform.position,Quaternion.identity);
            
            newItem.gameObject.transform.SetParent(instance.slotGrid.transform);
        newItem.SlotItem=item;
        newItem.SlotImage.sprite=item.itemImage;
        newItem.SlotNum.text=item.itemHeld.ToString();
        newItem.transform.localScale=new Vector3(1,1,1);
    }
    public static void RefreshItem()
        {
        for (int i = 0; i < instance.slotGrid.transform.childCount; i++)
			{
              if(instance.slotGrid.transform.childCount==0)break;
              Destroy(instance.slotGrid.transform.GetChild(i).gameObject);
        }
        for (int i = 0; i <instance.myBag.itemList.Count ; i++)
			{
             CreateNewItem(instance.myBag.itemList[i]);
			}
     }
}
